package com.liyiruo.testMain;

import com.liyiruo.service.impl.UserServiceImpl;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

/**
 * @author liyiruo
 * @Description
 * @Date 2023/4/18 下午10:44
 */
public class BeanFactoryTest {
    public static void main(String[] args) {
        //创建工厂对象
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        //创建一个xml读取器
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        //读取配置文件给工厂
        reader.loadBeanDefinitions("beans1.xml");
        //根据id获取bean实例
        UserServiceImpl bean = beanFactory.getBean(UserServiceImpl.class);
        UserServiceImpl userDao = beanFactory.getBean("userService", UserServiceImpl.class);
        UserServiceImpl userDao1 = (UserServiceImpl) beanFactory.getBean("userService");
        System.out.println(bean);
        System.out.println(userDao);
        System.out.println(userDao1);
    }
}
